package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.PayTypeAdapter;
import com.wnhz.work.yz.bean.PayTBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.NoScrollListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * ---付款方式
 */
public class PayTypeActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private ImageView iv_img1;
    private NoScrollListView my_listView;
    private PayTypeAdapter mAdapter;

    private List<PayTBean> beanList = new ArrayList<>();
//    private List<PayTBean> beanList2 = new ArrayList<>();
    private TextView tv_text;
    private static final String TAG = "PayTypeActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_type);

        initData();
        initTitle();
        initView();
    }

    private String name;
    private  String from;

    private void initData() {
        name = getIntent().getStringExtra("name");
        from = getIntent().getStringExtra("from");
    }

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("付款方式");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    private void initView() {
        findViewById(R.id.rl_all).setOnClickListener(this);
        iv_img1 = (ImageView) findViewById(R.id.iv_img1);
        my_listView = (NoScrollListView) findViewById(R.id.my_listView);
        tv_text = (TextView) findViewById(R.id.tv_text);

        if(from.equals("1")){
            upload();
        }else if(from.equals("2")){
            upTwoLoad();
        }


        mAdapter = new PayTypeAdapter(this, beanList, name);
        my_listView.setAdapter(mAdapter);

        my_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra("pay", beanList.get(position).getPay_name());
                intent.putExtra("payId", beanList.get(position).getPayway_id());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    private void upTwoLoad() {
        XUtil.Post(Url.ORDER_ZPAYWAY, null, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    JSONArray jsonArray = jsonObject.optJSONArray("info");
                    if (jsonArray != null) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            PayTBean bean = new PayTBean();
                            Gson gson = new Gson();
                            bean = gson.fromJson(jsonArray.optString(i), PayTBean.class);
                            beanList.add(bean);
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }


    private void upload() {
        XUtil.Post(Url.ORDER_GETPAYWAY, null, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    JSONArray jsonArray = jsonObject.optJSONArray("info");
                    if (jsonArray != null) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            PayTBean bean = new PayTBean();
                            Gson gson = new Gson();
                            bean = gson.fromJson(jsonArray.optString(i), PayTBean.class);
                            beanList.add(bean);
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //全款付清
            case R.id.rl_all:
                intent = new Intent();
                intent.putExtra("pay", "全款付清");
                intent.putExtra("payId", beanList.get(0).getPayway_id());
                setResult(RESULT_OK, intent);
                finish();
                break;

            //下一步
            case R.id.tv_next:
                MyToast("下一步");
                break;

            default:
                break;
        }
    }
}
